﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Utilities.Common;

namespace Utilities.UI
{
    public static class CheckBoxListExtensions
    {
        public static void Fill<U, T>(this CheckBoxList ddl, U datasource, string textField) where U : List<T>
        {
            if (datasource.Count != 0)
            {
                ClassInfo info = ClassInfoCache.GetClassInfo(datasource[0]);
                ddl.DataSource = datasource;
                ddl.DataValueField = info.Properties.PropId.Name;
                if (textField != "") ddl.DataTextField = textField;                
                // ddl.SelectedValue = "0";
            }
            else
            {
#warning ver
                ddl.DataSource = null;
            }

            ddl.DataBind();

        }
        public static U GetSelectedItems<U,T>(this CheckBoxList chks, U datasource)where U:List<T>,new() where T:new()
        {
            ClassInfo info = ClassInfoCache.GetClassInfo(new T());
            U list = new U();
            foreach (ListItem item in chks.Items)
            {
                if (item.Selected)
                {
                    list.Add(datasource.GetById<T>(item.Value));
                }
            }
            return list;
        }     
    }
}
